package com.bball.court;

import java.io.IOException;
import java.util.Properties;

import android.content.Context;
import android.content.res.Resources.NotFoundException;

public class FaCConfiguration {

    private final Properties properties = new Properties();
    private static FaCConfiguration faCConfiguration;

    private FaCConfiguration(Context context) throws NotFoundException, IOException {
        properties.load(context.getResources().openRawResource(R.raw.config));
    }

    public static FaCConfiguration getInstance(Context context) {
        if (faCConfiguration == null) {
            try {
                faCConfiguration = new FaCConfiguration(context);
            } catch (Exception e) {
                throw new RuntimeException("Invalid config file");
            }
        }
        return faCConfiguration;
    }

    public int getTimeOut() {
        return Integer.parseInt(properties.getProperty("service.timeout"));
    }

    public long getSplashScreenDuration() {
        return Long.parseLong(properties.getProperty("splash.duration"));
    }

    private String getGooleProfilePictureUrl(Context context, int resolution) {
        return String.format(properties.getProperty("profile.picture.google.url"), PreferenceHelper.getUserId(context), resolution);
    }

    private String getFacebookProfilePictureUrl(Context context, int resolution) {
        return String.format(properties.getProperty("profile.picture.facebook.url"), PreferenceHelper.getUserId(context), resolution, resolution);
    }

    private String getSmallGooleProfilePictureUrl(Context context) {
        return getGooleProfilePictureUrl(context, context.getResources().getInteger(R.integer.small_resolution));
    }

    private String getSmallFacebookProfilePictureUrl(Context context) {
        return getFacebookProfilePictureUrl(context, context.getResources().getInteger(R.integer.small_resolution));
    }

    private String getNormalGooleProfilePictureUrl(Context context) {
        return getGooleProfilePictureUrl(context, context.getResources().getInteger(R.integer.normal_resolution));
    }

    private String getNormalFacebookProfilePictureUrl(Context context) {
        return getFacebookProfilePictureUrl(context, context.getResources().getInteger(R.integer.normal_resolution));
    }

    public String getProfilePictureUrl(Context context) {
        switch (PreferenceHelper.getSocialMediaConnection(context)) {
        case Facebook:
            return getNormalFacebookProfilePictureUrl(context);
        case Google_plus:
            return getNormalGooleProfilePictureUrl(context);
        default:
            throw new IllegalStateException("Wrong social media : " + PreferenceHelper.getSocialMediaConnection(context));
        }
    }

    public String getSmallProfilePictureUrl(Context context) {
        switch (PreferenceHelper.getSocialMediaConnection(context)) {
        case Facebook:
            return getSmallFacebookProfilePictureUrl(context);
        case Google_plus:
            return getSmallGooleProfilePictureUrl(context);
        default:
            throw new IllegalStateException("Wrong social media : " + PreferenceHelper.getSocialMediaConnection(context));
        }
    }

}
